<?php
namespace Client\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class MaterialTable extends CommonTable
{
    protected $table = 'hy_material';
    protected $id = 'material_id';
    
    /**
     * 查询商品原料
     * @param number $limit
     * @param string $paginated
     */
    public function getMaterialByProductID($product_id) {
        $select = new Select($this->getTable());
        $select->columns( array(
            "material_id","material_code","material_name","product_id",
        ));
        $select->where(array('product_id' => $product_id));
        $rowset = $this->selectWith($select);
        $result = $rowset->toArray();
        return $result;
    }
    
    public function getItem($materialId){
    	if(empty($materialId)){
    		throw new \Exception("The material_id is invalid");
    	}
    	$select = new Select(array("mtrl"=>$this->getTable()));
    	$select->columns( array(
    		'material_id','material_category_id','product_id','material_code','material_name','original','description','workflow','feature','contact','create_time',
    		'material_supplier','update_time','address','material_supplier_img1','material_supplier_img2','material_supplier_img3','material_supplier_img4','material_supplier_img5',
    		'material_supplier_img6','material_supplier_img7','material_supplier_img8','material_production_date','material_yieldly','material_batch_no','material_expiry_date',
    		'material_other','material_img'
    	));
    	$select->join(array("ctgy"=>"hy_material_category"),"mtrl.material_category_id=ctgy.material_category_id",array('material_category_name'),$select::JOIN_LEFT);
    	$select->where(array('mtrl.material_id' => $materialId));
    	$rowset = $this->selectWith($select);
    	$result = $rowset->current();
    	return $result;
    }
    
    
    
    public function getCategoryMaterials($categoryIds){
		if(empty($categoryIds)){
			throw new \Exception("目录列表为空");
		}
    	$select = new Select($this->getTable());
    	$select->columns( array(
			'material_id', 'material_category_id', 'product_id', 'material_code', 'material_name', 'original', 'description', 'workflow', 'feature', 'contact', 'create_time', 'update_time'
    	));
    	$select->where(array('material_category_id' => $categoryIds));
    	$rowset= $this->selectWith($select);
    	$row = $rowset->toArray();
    	return $row;
    	
    }
    
    
}